package com.sumer.psychology.comment.infrastructure.messaging.rabbitmq;

import com.sumer.psychology.comment.application.command.CommentApplicationService;
import com.sumer.psychology.comment.application.command.DeleteCommentsCommand;
import com.sumer.psychology.common.notification.NotificationReader;
import com.sumer.psychology.common.port.adapter.messaging.Exchanges;
import com.sumer.psychology.common.port.adapter.messaging.rabbitmq.ExchangeListener;
import org.apache.commons.lang3.StringUtils;

/**
 * 评论删除监听
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/8/26
 * Time: 9:06
 */
public class CommentDeletedEventListener extends ExchangeListener {
    private CommentApplicationService commentApplicationService;

    public CommentDeletedEventListener(CommentApplicationService commentApplicationService) {
        this.commentApplicationService = commentApplicationService;
    }

    @Override
    protected String exchangeName() {
        return Exchanges.DEFAULT_EXCHANGE_NAME;
    }

    @Override
    protected void filteredDispatch(String aType, String aTextMessage) {
        NotificationReader reader = new NotificationReader(aTextMessage);
        String commentId = reader.eventStringValue("commentId.identity");

        if (StringUtils.isNotBlank(commentId)) {
            commentApplicationService().delete(
                    new DeleteCommentsCommand(commentId));
        }
    }

    @Override
    protected String[] listensTo() {
        return new String[]{"com.sumer.psychology.comment.domain.model.CommentDeletedEvent"};
    }

    public CommentApplicationService commentApplicationService() {
        return commentApplicationService;
    }
}
